package luigibros.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/** An ArrayList with the comparing method
 *  used fixed so that a list of objects
 *  that are able to be compared to String
 *  can be searched for a string.  This
 *  makes finding classes with a given "name"
 *  can be found with the <code>indexOf(Object)</code>
 *  method call, using the String as the Object.  
 *  
 *  This property is intended to facilitate
 *  searching lists based on properties of objects, 
 *  but this technique DOES violate the transitive
 *  property.  Please not that in using this method
 *  <code>obj1.equals(obj2)</code> may not be
 *  the same as <code>obj2.equals(obj1)</code>.  
 * @author R.Wisniewski1101
 * @param <T> - the type of elements in the list.  
 */
public class LazyList<T> extends ArrayList<T> {
	//Eclipse says I need this, because of Serializable.  Don't ask.  
	private static final long serialVersionUID = 5934458709971806494L;
	public LazyList() {}
	public LazyList(int capacity) { super(capacity); }
	public LazyList(Collection<T> collection) {super(collection);}
	@Override
	public int indexOf(Object obj) {
		for(int x = 0; x < size(); ++x)
			//fixed the comparison order.  muahaha
			if(get(x) == null ? obj == null : get(x).equals(obj))
				return x;
		return -1;
	}
	@Override
	public boolean contains(Object obj) {
		return indexOf(obj) != -1;
	}
	
	public Iterable<T> inReversedOrder() { return new Iterable<T>() {
			@Override
			public Iterator<T> iterator() { return new Iterator<T>() {
					private int current = size() -1;
					@Override
					public boolean hasNext() {return current >= 0;}
					@Override
					public T next() {return get(current--);}
					@Override
					public void remove() {throw new UnsupportedOperationException();}
				};
			}
		};
	}
}
